//
//  MineViewController.swift
//  daily_count
//
//  Created by 段帅 on 2018/4/2.
//  Copyright © 2018年 Sooc. All rights reserved.
//

import UIKit

class MineViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    @IBOutlet weak var name_label: UILabel!
    
    @IBOutlet weak var phone_label: UILabel!
    
    @IBOutlet weak var age_label: UILabel!
    
    @IBOutlet weak var header_image: UIImageView!
    let image_big = UIImageView()
    let text_uuid = UITextView();
    
    
    
    @IBAction func quite(_ sender: Any) {
        APP_DELEGATION.quite();
    }
    
    

    @IBAction func getUuid(_ sender: Any) {
        let idfv = UIDevice.current.identifierForVendor?.uuidString;
        
        text_uuid.frame = CGRect.init(x: 30, y: 100, width: SIZE_WITH-60, height: 100);
        text_uuid.font = UIFont.systemFont(ofSize: 20);
        view.addSubview(text_uuid);
        text_uuid.text = idfv;
        
        
        let button = UIButton.init(type: UIButtonType.custom);
        button.setTitle("关闭", for: UIControlState.normal);
        button.frame = CGRect.init(x: 0, y: 80, width: SIZE_WITH-60, height: 20);
        button.setTitleColor(UIColor.red, for: UIControlState.normal);
        text_uuid.addSubview(button);
        button.addTarget(self, action: #selector(hideUUID), for: UIControlEvents.touchUpInside);
        
        
        
        
    }
    
    @objc func hideUUID() {
        text_uuid.removeFromSuperview();
    }
    

    
    
    
    
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        image_big.isUserInteractionEnabled = true;
        image_big.frame = CGRect.init(x: 0, y: 100, width: SIZE_WITH, height: SIZE_WITH);
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(hideImage));
        image_big.addGestureRecognizer(tap);
        
        // Do any additional setup after loading the view.
        
    }
    @objc func hideImage()  {
        image_big.removeFromSuperview();
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        createData();
        self.header_image.layer.cornerRadius = 34;
        self.header_image.layer.masksToBounds = true;
        self.header_image.layer.borderColor = UIColor.white.cgColor;
        self.header_image.layer.borderWidth = 2;
       
        
        
        
        
        
        
    }
  

    func createData()  {
        DSToolManager.postWithToken(URLString: severseUrl + api_getUserMessage, parameters: nil) { (data, isSucess) in
            if isSucess {
               
                let message = data!["data"] as!  Dictionary<String,AnyObject> ;
                
                
                
                self.name_label.text = message["name"] as? String;
                self.phone_label.text = message["phone"] as? String;
                self.age_label.text = (message["age"] as? String);
                self.header_image.sd_setImage(with: URL.init(string: (message["image"] as? String)!), completed: nil);
                
            }else{
                DSToolManager.alertFaild(message: data!["statusCode"] as! String);
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func selectImage(_ sender: UITapGestureRecognizer) {
        
        print("上传图片");
        let sexActionSheet = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
        
        
        
        let sexNanAction = UIAlertAction(title: "从相册中选择", style: UIAlertActionStyle.default){ (action:UIAlertAction)in
            
            self.initPhotoPicker()
            //填写需要的响应方法
            
        }
        
        let sexNvAction = UIAlertAction(title: "拍照", style: UIAlertActionStyle.default){ (action:UIAlertAction)in
   
            self.initCameraPicker()
            //填写需要的响应方法
            
        }
        let lookAction = UIAlertAction(title: "查看大图", style: UIAlertActionStyle.default){ (action:UIAlertAction)in
            
            
            //填写需要的响应方法
            self.image_big.image = self.header_image.image;
            self.view.addSubview(self.image_big);
            
        }
        
        let sexSaceAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel){ (action:UIAlertAction)in
            
            //填写需要的响应方法
            
        }
        
        
        sexActionSheet.addAction(sexNvAction)
        sexActionSheet.addAction(sexNanAction)
        sexActionSheet.addAction(lookAction)
        sexActionSheet.addAction(sexSaceAction)
        
        self.present(sexActionSheet, animated: true, completion: nil)
        
    }
    
    //MARK: - 相机
    
    //从相册中选择
    func initPhotoPicker(){
        let photoPicker =  UIImagePickerController()
        photoPicker.delegate = self
        photoPicker.allowsEditing = true
        photoPicker.sourceType = .photoLibrary
        //在需要的地方present出来
        self.present(photoPicker, animated: true, completion: nil)
    }
    
    //拍照
    func initCameraPicker(){
        
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            let  cameraPicker = UIImagePickerController()
            cameraPicker.delegate = self
            cameraPicker.allowsEditing = true
            cameraPicker.sourceType = .camera
            //在需要的地方present出来
            self.present(cameraPicker, animated: true, completion: nil)
        } else {
            
            print("不支持拍照")
            
        }
        
    }
    
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        
        
        //获得照片
        let image:UIImage = info[UIImagePickerControllerEditedImage] as! UIImage
        
        // 拍照
        if picker.sourceType == .camera {  
            //保存相册
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)   
        }
        
        DSToolManager.uploadImage(URLString:severseUrl+api_upload, parameters: nil, image: image) { (data:AnyObject?, sucess:Bool) in
            if sucess {
                let message = data!["data"] as!  Dictionary<String,String>;
                self.header_image.sd_setImage(with: URL.init(string: message["adr"]!), completed: nil);
            }else {
                DSToolManager.alertFaild(message: data!["statusCode"] as! String); 
            }
        }
        
        self.dismiss(animated: true, completion: nil)
    }
    
    @objc func image(image:UIImage,didFinishSavingWithError error:NSError?,contextInfo:AnyObject) {
        
        if error != nil {
            
            print("保存失败")
            
            
        } else {
            
            print("保存成功")
            
            
        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
